package cn.thornbird.orgsync.repository.specification;

import cn.thornbird.orgsync.entity.LoginLog;
import cn.thornbird.orgsync.vo.request.LoginLogQueryRequest;
import lombok.AllArgsConstructor;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@AllArgsConstructor
public class LoginLogSpecification extends BaseSpecification<LoginLog> {

    private static final long serialVersionUID = 2335278764114322157L;

    private final LoginLogQueryRequest loginLogQueryRequest;

    @Override
    public Predicate toPredicate(Root<LoginLog> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
        List<Predicate> predicates = new ArrayList<>();
        String username = loginLogQueryRequest.getUsername();
        Date createdAt = loginLogQueryRequest.getCreatedAt();
        predicates.add(createEqualPredicate(root, criteriaBuilder, "username", username.toLowerCase(), true));
        predicates.add(createDatePredicates(root, criteriaBuilder, "createdAt", createdAt));
        return toPredicate(criteriaBuilder, predicates);
    }

}
